package ee.android.kinokavad;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import ee.android.kinokavad.data.Movie;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.TextView;

public class BigMovieActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.movie_item);
		
		Intent i = getIntent();
		final Movie movie = (Movie)i.getSerializableExtra("movie");
		
		TextView titleView = (TextView) findViewById(R.id.movieBigTitle);
		if (!movie.getTitle().equals("")){
			titleView.setText(movie.getTitle());
		}
		
		TextView descriptionView = (TextView) findViewById(R.id.movieBigDescription);
		if (!movie.getDescription().equals("")){
			descriptionView.setText(movie.getDescription());
		}
		TextView genreView = (TextView) findViewById(R.id.moveiGenreTextView);
		if (!movie.getGenre().equals("")) {
			genreView.setText(movie.getGenre());
		}
		final ImageView imdb = (ImageView) findViewById(R.id.imdbImage);
		imdb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!movie.getImdbLink().equals("")){
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movie.getImdbLink()));
					startActivity(browserIntent);
				} else {
					Toast.makeText(BigMovieActivity.this, "Kahjuks see link puudub", Toast.LENGTH_SHORT).show();
				}
			}
		});
		final ImageView youtube = (ImageView) findViewById(R.id.youtubeImage);
		youtube.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!movie.getYoutubeLink().equals("")){
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movie.getYoutubeLink()));
					startActivity(browserIntent);
				} else {
					Toast.makeText(BigMovieActivity.this, "Kahjuks see link puudub", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		
		new Thread(new Runnable() {
			public void run() {
				final ImageView image = (ImageView) findViewById(R.id.movieImageView);
				final Bitmap bitmap = loadBitmap(movie.getPictureLink());
				runOnUiThread(new Runnable() {
					public void run() {

						if (bitmap != null) {
							Log.d("HEIGHT",bitmap.getWidth() / bitmap.getHeight()+"");
							if (bitmap.getWidth()/bitmap.getHeight() == 2 && 
									BigMovieActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
								float d = BigMovieActivity.this.getResources().getDisplayMetrics().density;
										int height = (int)(bitmap.getHeight() * d);
								image.getLayoutParams().height = height;
							}
							image.setImageBitmap(bitmap);
						} else {
							TextView v = (TextView) findViewById(R.id.moviePaddingView);
							v.setVisibility(View.VISIBLE);
							image.setVisibility(View.GONE);
						}

					}
				});

			}
		}).start();
		
		
		
		
	}

	public static Bitmap loadBitmap(String url) {
		URL newurl = null;
		Bitmap bitmap = null;
		try {
			newurl = new URL(url);
			bitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	    return bitmap;
	}
}
